<template>
	<view class="container">
		<address-book v-if="bookList.length > 0" :bookList="bookList" :letter="letter">
			<template v-slot:addressBookList>
				<view class="bookList">
					<block v-for="item in bookList" :key="item.key">
						<view :class="['bookKey']" v-if="item.key !== '#'">{{item.key}}</view>
						<block v-for="items in item.list" :key="items.id">
							<view class="book" @tap="jump(items.id,items.nickname)">
								<view class="headimg">
									<image :src="items.avatar" mode="widthFix"></image>
								</view>
								<view class="font36 name">{{items.nickname}}</view>
							</view>
						</block>
					</block>
				</view>
			</template>
		</address-book>
	</view>
</template>

<script>
	import AddressBook from '../../components/addressBookNavigation/addressBookNavigation.vue'
	import {makePy} from '../../utils/pinyin.js'
	export default {
		components:{AddressBook},
		data() {
			return {
				letter:[],
				addFriend: {
					key: '#',
					list: [
						{
							id:-1,
							nickname:"添加好友",
							avatar:"../../static/image/ChatList/addgroup@2x.png"
						},
						{
							id:-2,
							nickname:"群聊",
							avatar:"../../static/image/communities.png"
						}
					]
				},
				list:[],
				bookList:[]
			}
		},
		created() {
			this.getFriendsList()
		},
		methods: {
			// 对通讯录按字母顺序做排序
			letterSrot(){
				const sortList = {}
				this.list.forEach(item => {
					const first = makePy(item.nickname).substr(0,1)
					if (sortList[first]) {
						sortList[first].push(item)
					} else {
						sortList[first] = [item]
					}
				})
				this.letter = Object.keys(sortList).sort()
				for (var i in this.letter) {
					var key = this.letter[i]
					const obj = {}
					obj.key = key
					obj.list = sortList[key]
					this.bookList.push(obj)
				}
				this.letter.unshift('#')
				this.bookList.unshift(this.addFriend)
			},
			jump(id,nickname){
				console.log(`jump:id=${id},${nickname}`)
				if (id == -1) {
					this.searchFriend()
				} else if (id == -2) {
					this.goGroupList()
				} else {
					this.goChat(id)
				}
			},
			async getFriendsList() {
				let res = await this.$u.api.friendsList()
				if (res != '') {
					this.list = res.data
					this.letterSrot()
					let data = this.$utils.reduceData(res.data)
					uni.setStorageSync(this.$config.friends_list_key, data);
				}
			},
			searchFriend() {
				this.$u.route('/pages/addressBook/search/search')
			},
			goChat(dstId) {
				this.$u.route('/pages/message/chat/chat', {
					dstId: dstId
				})
			},
			goGroupList() {
				this.$u.route('/pages/addressBook/groupList/groupList')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bookList{
		margin-top: 0;
		padding-left: 40rpx;
		.bookKey{
			padding-top: 20rpx;
			line-height: 60rpx;
			color:#20BB5B;
		}
		.book{
			height: 120rpx;
			line-height: 120rpx;
			display: flex;
			flex-direction: row;
			.headimg{
				width: 100rpx;
				image{
					width: 88rpx;
					height: 88rpx;
					border-radius: 4rpx;
					vertical-align: middle;
				}
			}
			.name{
				padding-left: 20rpx;
				flex:1;
				border-bottom:1px solid #F3F3F3;
			}
		}
		.book:last-child .name{
			border-bottom:0;
		}
	}

	.list {
		.item {
			display: flex;
			align-items: center;
			padding: 20rpx 32rpx;
			.head {
				position: relative;
				.head-img {
					background: $gg-color;
					border-radius: 24rpx;
					width: 96rpx;
					height: 96rpx;
				}
				.red-dot {
					position: absolute;
					left: 68rpx;
					top: -8rpx;
					padding: 2rpx 10rpx;
					background: #ff5d5b;
					border-radius: 18rpx;
					font-size: 24rpx;
					line-height: 32rpx;
					color: #ffffff;
					letter-spacing: -0.41rpx;
				}
			}
			.info {
				margin-left: 32rpx;
				width: 100%;
				.top {
					display: flex;
					justify-content: space-between;
					align-items: center;
					.left {
						font-size: 36rpx;
						color: #272832;
						letter-spacing: -1.23rpx;
						line-height: 50rpx;
					}
					.time {
						font-size: 24rpx;
						color: rgba(39, 40, 50, 0.4);
						letter-spacing: -0.82rpx;
						line-height: 34rpx;
					}
				}
				.bottom {
					font-size: 28rpx;
					color: rgba(39, 40, 50, 0.6);
					letter-spacing: -0.96rpx;
					line-height: 40rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
				}
			}
		}
	}
	
	.book-list {
		.title {
			padding: 34rpx 32rpx 0;
			font-size: 44rpx;
			color: #272832;
			letter-spacing: -1.5rpx;
			line-height: 60rpx;
		}
		.item {
			padding: 20rpx 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.left{
				display: flex;
				align-items: center;
				.head{
					width: 80rpx;
					height: 80rpx;
					border-radius: 20rpx;
				}
				.name {
					margin-left: 32rpx;
					font-size: 36rpx;
					color: #272832;
					letter-spacing: -1.24rpx;
				}
			}
			.right {
				.sendMsg {
					background: $gg-color;
					border-radius: 24rpx;
					padding: 8rpx 24rpx;
					font-size: 24rpx;
					color: #FFFFFF;
					letter-spacing: -0.82rpx;
					line-height: 34rpx;
				}
			}
		}
	}
</style>
